<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="inc/default :: default"></head>
<body style="background-color: #fff">
<form class="layui-form" action="" lay-filter="form1">
    <div class="layui-tab-item layui-show">
        <div class="layui-card">
            <div class="layui-card-header">[[${systemRole.name}]]</div>
            <div class="layui-card-body">
                <ul id="tree1" class="ztree"></ul>
            </div>
        </div>
    </div>
    <button type="submit" class="layui-btn" lay-submit lay-filter="submit1" style="display:none;"></button>
</form>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var ctxPath = /*[[@{/}]]*/ '';
    /*]]>*/
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引

    var treeObj;

    var setting = {
        async: {
            enable: true
            ,url: ctxPath + 'systemRole/menuList'
            ,otherParam: {"roleId": [[${systemRole.roleId}]]}
        },
        view: {
            showLine: false,
            showIcon: function(treeId, treeNode) {
                return !treeNode.isParent;
            }
        },
        data: {
            simpleData: {
                enable: true
                ,idKey: "menuId"
                ,pIdKey: "pid"
            }
        },
        check: {
            enable: true
        },
        callback:{
            onAsyncSuccess: function() {
                treeObj.expandAll(true);
            }
        }
    };

    ;!function(){
        var layer1 = layui.layer
            ,form = layui.form;

        treeObj = $.fn.zTree.init($("#tree1"), setting);

        form.on('submit(submit1)', function(data){
            var nodes = treeObj.getCheckedNodes(true);
            var pks = [];
            $.each(nodes, function (index, data) {
                pks.push(data.menuId);
            });
            $.ajax({
                type: 'post',
                url : ctxPath + 'systemRole/saveMenus',
                data : {
                    roleId : [[${systemRole.roleId}]],
                    menuIds : pks.join(",")
                },
                success : function(result) {
                    if (result) {
                        layer1.msg("设置角色菜单成功！", {
                            icon : 1,
                            time : 2000
                        }, function() {
                            if (index) {
                                parent.layer.close(index);
                            }
                        });
                    } else {
                        layer1.msg("设置角色菜单失败！", {
                            icon : 2,
                            anim : 6,
                            time : 2000
                        });
                    }
                },
                error : function() {
                    layer1.msg("设置角色菜单失败！", {
                        icon : 2,
                        anim : 6,
                        time : 2000
                    });
                }
            });
            return false;
        });
    }();
</script>